package org.hgvc.webapp.ets.spring.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import org.hgvc.webapp.ets.bo.ActionBulletin;
import org.hgvc.webapp.ets.bo.CalendarCategory;
import org.hgvc.webapp.ets.bo.CalendarCategoryDtl;
import org.hgvc.webapp.ets.bo.CalendarDtl;
import org.hgvc.webapp.ets.bo.DocControl;
import org.hgvc.webapp.ets.bo.Member;
import org.hgvc.webapp.ets.bo.Partner;
import org.hgvc.webapp.ets.bo.PartnerDocument;
import org.hgvc.webapp.ets.bo.PartnerDocumentCheckList;
import org.hgvc.webapp.ets.bo.PartnerProduct;
import org.hgvc.webapp.ets.bo.PartnerProductBooking;
import org.hgvc.webapp.ets.bo.PartnerProductExtension;
import org.hgvc.webapp.ets.bo.PartnerProductType;
import org.hgvc.webapp.ets.bo.XSServiceType;
import org.hgvc.webapp.ets.constant.ActionBulletinConstant;
import org.hgvc.webapp.ets.constant.CommonConstant;
import org.hgvc.webapp.ets.constant.PartnerConstant;
import org.hgvc.webapp.ets.constant.SystemConstant;
import org.hgvc.webapp.ets.constant.TerminateConstant;
import org.hgvc.webapp.ets.jsf.beans.admin.AdminPartnerApprovalBean;
import org.hgvc.webapp.ets.jsf.beans.admin.AdminPartnerProductApprovalBean;
import org.hgvc.webapp.ets.jsf.beans.partner.PartnerProductApprovalBean;
import org.hgvc.webapp.ets.spring.dao.PartnerHbmDao;
import org.hibernate.Query;
import org.jocsb.base.bo.User;
import org.jocsb.base.exception.BaseException;
import org.jocsb.base.util.BeanUtil;
import org.jocsb.base.util.SpringUtils;
import org.jocsb.base.util.SystemUtil;

import com.j6.framework.util.DateUtil;
import com.j6.framework.util.ReflectionUtil;


public class PartnerServiceImpl implements PartnerService{

	private PartnerHbmDao dao;
	private AdminService adminService;
	private MasterSetupService setupService;
	private SystemService systemService;

	 
	public List<CalendarCategoryDtl> getCalendarCategoryDtls(String calendarCatId)throws Exception{
		return dao.getCalendarCategoryDtls(calendarCatId);
	}
	
	public void setSystemService(SystemService systemService) {
		this.systemService = systemService;
	}

	public void setDao(PartnerHbmDao dao) {
		this.dao = dao;
	}

	public void setAdminService(AdminService adminService) {
		this.adminService = adminService;
	}

	public void setSetupService(MasterSetupService setupService) {
		this.setupService = setupService;
	}

	public String partnerRegistration(Partner partner,boolean isOnlienReg,User creadtedUser)throws BaseException{
		try{
			if(isOnlienReg){
				partner.setRegType(PartnerConstant.PARTNER_REG_ONLINE);
			}else{
				partner.setRegType(PartnerConstant.PARTNER_REG_OFFLINE);
				partner.setCreatedBy(creadtedUser);
			}
			partner.setCreatedDate(new Date());
			partner.setStatus(PartnerConstant.PARTNER_STATUS_PENDING_APPROVAL_REG);
			//save partner
			String regNo = getDocumentNo(CommonConstant.DOC_TYPE_PARTNER_REG);
			partner.setPartnerRegNo(regNo);
			
			if (partner.getMailCountry().getCountryId()==null)
				partner.setMailCountry(null);
			if (partner.getMailState().getStateId()==null)
				partner.setMailState(null);
			
			//create temp user for particular user
			User user = adminService.createNewUserWithRole(partner.getEmail(), SystemConstant.DEFAULT_PASSWORD
					, SystemConstant.PARTNER_TEMP_ROLE, SystemConstant.SYSTEM_USER);
			partner.setRefUser(user);
			dao.save(partner);
			//create action bullentin to admin
			ActionBulletin ab = new ActionBulletin();
			ab.setActionCode(SpringUtils.getMessage("ab.partner.reg.pending.approval", new Object[]{partner.getPartnerRegNo()}));
			ab.setActionParam1(partner.getPartnerId());
			ab.setActionNo(SystemUtil.getUniqueCode());
			ab.setActionBean(AdminPartnerApprovalBean.BEAN_NAME);
			ab.setActionMethod(AdminPartnerApprovalBean.AB_INVOKE_ACTION);
			ab.setActionParam1(partner.getPartnerId());
			ab.setIsDeleted(CommonConstant.NOT_DELETED);
			ab.setType(ActionBulletinConstant.AB_ACK_TYPE);
			ab.setCreatedBy(SystemConstant.SYSTEM_USER);
			ab.setCreatedDate(new Date());
			
			systemService.createActionBulletinByRoles(ab, SystemConstant.ADMIN_ROLE);
			systemService.createActionBulletinByRoles(ab, SystemConstant.MEMBER_SERVICE_ROLE);//Added by jacky 19-OCT-2008 2131
			
			return regNo;
		}catch(BaseException be){
			be.printStackTrace();
			throw be;
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.partner.registration", new Object[]{}));
		}
	}

	public Partner getPartnerByUserId(String userId)throws BaseException{
		try{
			return dao.getPartnerByUserId(userId);
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.get.partner", new Object[]{}));
		}
	}
	
	public void approvePartnerRegistration(Partner partner, User approver,String reason,String code,String actionNo)throws BaseException{
		try{
			//duplicate code
			List<Partner> list = dao.getPartnerByCode(code);
			if(list.size()>0){
				throw new BaseException(SpringUtils.getMessage("error.duplicate.partner.code", new Object[]{}));
			}
			
			partner.setStatus(PartnerConstant.PARTNER_STATUS_PENDING_CHECK_LIST);
			partner.setModifiedBy(approver);
			partner.setModifiedDate(new Date());
			partner.setReason(reason);
			partner.setCode(code);
			dao.update(partner);
			
			//create partner doc list
			List<PartnerDocument> docList = dao.getPatnerDocumentList();
			for(PartnerDocument doc:docList){
				PartnerDocumentCheckList checkList = new PartnerDocumentCheckList();
				checkList.setCreatedBy(SystemConstant.SYSTEM_USER);
				checkList.setCreatedDate(new Date());
				checkList.setIsDeleted(CommonConstant.NOT_DELETED);
				checkList.setDocument(doc);
				checkList.setPartner(partner);
				checkList.setFlag(PartnerConstant.PARTNER_DOC_STATUS_UNCOMPLETE);
				dao.save(checkList);
			}
			
			systemService.deleteActionBulletinByActionNo(actionNo);
			
		}catch(BaseException be){
			be.printStackTrace();
			throw be;
		}
		catch(Exception be){
			be.printStackTrace();
			throw new BaseException("error.approve.partner.registration");
		}
	}
	
	public void rejectPartnerRegistration(Partner partner, User approver,String reason,String actionNo)throws BaseException{
		try{
			partner.setStatus(PartnerConstant.PARTNER_STATUS_REJECT_REG);
			partner.setModifiedBy(approver);
			partner.setModifiedDate(new Date());
			partner.setReason(reason);
			dao.update(partner);
			
			systemService.deleteActionBulletinByActionNo(actionNo);
		}catch(Exception be){
			be.printStackTrace();
			throw new BaseException("error.reject.partner.registration");
		}
	}
	
	
	public Partner getPartnerById(String partnerId)throws BaseException{
		try{
			return (Partner) dao.getObject(Partner.class, partnerId);
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException("error.get.partner");
		}
	}
	
	public synchronized String getDocumentNo(String type)throws BaseException{
		String documentNo="";
		try{
			DocControl docControl = setupService.getDocControlByType(type);
			if (docControl==null)
				throw new BaseException(SpringUtils.getMessage("error.doc.type.not.config", new Object[]{type}));

			StringBuffer numberBuf =new StringBuffer();
			numberBuf.append(docControl.getPrefix());
			long counter = docControl.getNumber()+1;
			long currentSize=new Long(String.valueOf(counter).length()).longValue();
			long size = docControl.getSize();
			for (long l=currentSize;l<size;l++){
				numberBuf.append("0");
			}
			numberBuf.append(counter);
			documentNo = numberBuf.toString();
			docControl.setNumber(counter);
			setupService.updateDocControl(docControl);
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.generate.doc.no", new Object[]{type}));
		}
		return documentNo;
	}
	
	public List<Partner> getPendingApprovalPartner()throws BaseException{
		try{
			
			List<String> sList = new ArrayList<String>();
			sList.add(PartnerConstant.PARTNER_STATUS_PENDING_APPROVAL_REG);
			sList.add(PartnerConstant.PARTNER_STATUS_PENDING_CHECK_LIST);
			
			return dao.getPartnerListWithStatus(sList,null,null);
			
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.get.pending.approval.partner.list", new Object[]{}));
		}
	}
	
	//Added By Jacky 28-03-2008-1032
	public List<Partner> getApprovedPartnerList()throws BaseException{
		try{
			List<String> sList = new ArrayList<String>();
			sList.add(PartnerConstant.PARTNER_STATUS_APPROVE_PARTNER);
			
			return dao.getPartnerListWithStatus(sList,null,null);
			
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.get.pending.approval.partner.list", new Object[]{}));
		}
	}
	
	public List<Partner> getAllPartnerList()throws BaseException{
		try{
			List<String> sList = new ArrayList<String>();
			sList.add(PartnerConstant.PARTNER_STATUS_APPROVE_PARTNER);
			sList.add(PartnerConstant.PARTNER_STATUS_PENDING_APPROVAL_REG);
			sList.add(TerminateConstant.TERMINATED);
			return dao.getPartnerListWithStatus(sList,null,null);
			
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.list.object", new Object[]{Partner.class.getSimpleName()}));
		}
	}
	public List<Partner> getAllActivePartnerList()throws BaseException{
		try{
			List<String> sList = new ArrayList<String>();
			sList.add(PartnerConstant.PARTNER_STATUS_APPROVE_PARTNER);
		//	sList.add(PartnerConstant.PARTNER_STATUS_PENDING_APPROVAL_REG);
		//	sList.add(TerminateConstant.TERMINATED);
			return dao.getPartnerListWithStatus(sList,null,null);
			
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.list.object", new Object[]{Partner.class.getSimpleName()}));
		}
	}
	public List<Partner> searchPartnerList(String code,String companyName)throws BaseException{
		
		List<Partner> plist = new ArrayList<Partner>();
		
		try{
			List<String> sList = new ArrayList<String>();
			sList.add(PartnerConstant.PARTNER_STATUS_APPROVE_PARTNER);
			sList.add(PartnerConstant.PARTNER_STATUS_PENDING_APPROVAL_REG);
			sList.add(TerminateConstant.TERMINATED);
			plist=dao.getPartnerListWithStatus(sList, code, companyName);
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.list.object", new Object[]{Partner.class.getSimpleName()}));
		}
		/*if(regno==null){
			regno="";
		}
		if(name==null){
			name="";
		}
		
		if(name.length()==0&&regno.length()==0){
			return partnerList;
		}
		
		for(Partner p :partnerList){
			if(p.getPartnerRegNo().equalsIgnoreCase(regno)){
				plist.add(p);
				continue;
			}
			if(p.getCompName().equalsIgnoreCase(name)){
				plist.add(p);
				continue;
			}
		}*/
		
		return plist;
		
	}
	
	public List<PartnerDocumentCheckList> getPartnerDocumentCheckListWithType(String type,String partnerId)throws BaseException{
		try{
			return dao.getPartnerDocumentCheckListWithType(type, partnerId);
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.get.partner.document.check.list", new Object[]{}));
		}
	}
	
	public void checkPartnerDocumentSubmission(List<PartnerDocumentCheckList> mlist, List<PartnerDocumentCheckList> nlist, User user)throws BaseException{
		try{
			
			for(PartnerDocumentCheckList doc:mlist){
				doc.setModifiedBy(user);
				doc.setModifiedDate(new Date());
				dao.update(doc);
			}
			
			for(PartnerDocumentCheckList doc:nlist){
				doc.setModifiedBy(user);
				doc.setModifiedDate(new Date());
				dao.update(doc);
			}
			
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException("error.check.partner.document.check.list");
		}
	}
	
	public void completePartnerDocumentSubmission(Partner partner,List<PartnerDocumentCheckList> mlist, List<PartnerDocumentCheckList> nlist, User user)throws BaseException{
		try{
			checkPartnerDocumentSubmission(mlist,nlist,user);
			partner.setStatus(PartnerConstant.PARTNER_STATUS_APPROVE_PARTNER);
			partner.setModifiedBy(user);
			partner.setModifiedDate(new Date());
			dao.merge(partner);
			adminService.replaceUserRoleWithNewRole(partner.getRefUser(), SystemConstant.PARTNER_ROLE, SystemConstant.SYSTEM_USER);
			
		}catch(BaseException ex){
			ex.printStackTrace();
			throw ex;
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.complete.partner.document.check.list", new Object[]{}));
		}
	}
	//partner master
	//partner document
	//list
	public List<PartnerDocument> getPartnerDocumentList()throws BaseException{
		try{
			return dao.getPatnerDocumentList();
		}catch(BaseException ex){
			ex.printStackTrace();
			throw ex;
		}
		catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.list.partner.doc", new Object[]{}));
		}
	}
	
	//add
	public void createPatnerDocument(PartnerDocument doc,User createdUser)throws BaseException{
		try{
			
			//check duplicate
			List<PartnerDocument> list = dao.getPatnerDocumentListByNameOrCode(doc.getName(), doc.getCode());
			if(list.size()>0){
				throw new BaseException(SpringUtils.getMessage("error.duplicate.partner.doc", new Object[]{}));
			}
			
			doc.setCreatedBy(createdUser);
			doc.setCreatedDate(new Date());
			doc.setIsDeleted(CommonConstant.NOT_DELETED);
			dao.save(doc);
			
		}catch(BaseException ex){
			ex.printStackTrace();
			throw ex;
		}
		catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.create.partner.doc", new Object[]{}));
		}
	}
	
	//edit
	public void updatePatnerDocument(PartnerDocument doc,User createdUser)throws BaseException{
		try{
			
			//check duplicate
			List<PartnerDocument> list = dao.getPatnerDocumentListByNameOrCode(doc.getName(), doc.getCode());
			if(list.size()>0){
				for(PartnerDocument cdoc:list){
					if(!cdoc.getPartnerDocId().equalsIgnoreCase(doc.getPartnerDocId())){
						throw new BaseException(SpringUtils.getMessage("error.duplicate.partner.doc", new Object[]{}));
					}
				}
			}
			dao.clear();
			doc.setModifiedBy(createdUser);
			doc.setModifiedDate(new Date());
			dao.update(doc);
		}catch(BaseException ex){
			ex.printStackTrace();
			throw ex;
		}
		catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.edit.partner.doc", new Object[]{}));
		}
	}
	//delete
	public void deletePatnerDocument(List<PartnerDocument> deleteList,User createdUser)throws BaseException{
		try{
			for(PartnerDocument doc:deleteList){
				if(doc.isMarkAsAction()){
					doc.setIsDeleted(CommonConstant.IS_DELETED);
					doc.setModifiedBy(createdUser);
					doc.setModifiedDate(new Date());
					dao.update(doc);
				}
			}
		}catch(BaseException ex){
			ex.printStackTrace();
			throw ex;
		}
		catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.delete.partner.doc", new Object[]{}));
		}
	}
	
	//partner product type
	//list
	public List<PartnerProductType> getPartnerProductTypeList()throws BaseException{
		try{
			return dao.getPartnerProductTypeList();
		}catch(BaseException ex){
			ex.printStackTrace();
			throw ex;
		}
		catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.list.partner.product.type", new Object[]{}));
		}
	}
	
	//add
	public void createPartnerProductType(PartnerProductType type,User createdUser)throws BaseException{
		try{
			//check duplicate
			List<PartnerProductType> list = dao.getPartnerProductTypeListByNameOrCode(type.getName(), type.getCode());
			if(list.size()>0){
				throw new BaseException(SpringUtils.getMessage("error.duplicate.partner.product.type", new Object[]{}));
			}
			type.setCreatedBy(createdUser);
			type.setCreatedDate(new Date());
			type.setIsDeleted(CommonConstant.NOT_DELETED);
			dao.save(type);
			
		}catch(BaseException ex){
			ex.printStackTrace();
			throw ex;
		}
		catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.create.partner.product.type", new Object[]{}));
		}
	}
	
	//edit
	public void updatePartnerProductType(PartnerProductType type,User createdUser)throws BaseException{
		try{
			
			//check duplicate
			List<PartnerProductType> list = dao.getPartnerProductTypeListByNameOrCode(type.getName(), type.getCode());
			if(list.size()>0){
				for(PartnerProductType t:list){
					if(!t.getPartnerProductTypeId().equalsIgnoreCase(type.getPartnerProductTypeId())){
						throw new BaseException(SpringUtils.getMessage("error.duplicate.partner.product.type", new Object[]{}));
					}
				}
			}
			dao.clear();
			type.setModifiedBy(createdUser);
			type.setModifiedDate(new Date());
			dao.update(type);
		}catch(BaseException ex){
			ex.printStackTrace();
			throw ex;
		}
		catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.edit.partner.product.type", new Object[]{}));
		}
	}
	//delete
	public void deletePartnerProductType(List<PartnerProductType> deleteList,User createdUser)throws BaseException{
		try{
			for(PartnerProductType t:deleteList){
				if(t.isMarkAsAction()){
					t.setIsDeleted(CommonConstant.IS_DELETED);
					t.setModifiedBy(createdUser);
					t.setModifiedDate(new Date());
					dao.update(t);
				}
			}
		}catch(BaseException ex){
			ex.printStackTrace();
			throw ex;
		}
		catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.delete.partner.product.type", new Object[]{}));
		}
	}
	
	//partner calendar category
	//list
	public List<CalendarCategory> getCalendarCategoryList()throws BaseException{
		try{
			return dao.getCalendarCategoryList();
		}catch(BaseException ex){
			ex.printStackTrace();
			throw ex;
		}
		catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.list.partner.calendar.category", new Object[]{}));
		}
	}
	
	//add
	public void createCalendarCategory(CalendarCategory cat,User createdUser)throws BaseException{
		try{
			//check duplicate
			List<CalendarCategory> list = dao.getCalendarCategoryListByNameOrCode(cat.getName(), cat.getCode());
			if(list.size()>0){
				throw new BaseException(SpringUtils.getMessage("error.duplicate.partner.calendar.category", new Object[]{}));
			}
			cat.setCreatedBy(createdUser);
			cat.setCreatedDate(new Date());
			cat.setIsDeleted(CommonConstant.NOT_DELETED);
			dao.save(cat);
			
		}catch(BaseException ex){
			ex.printStackTrace();
			throw ex;
		}
		catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.create.partner.calendar.category", new Object[]{}));
		}
	}
	
	//edit
	public void updateCalendarCategory(CalendarCategory cat,User createdUser)throws BaseException{
		try{
			
			//check duplicate
			List<CalendarCategory> list = dao.getCalendarCategoryListByNameOrCode(cat.getName(), cat.getCode());
			if(list.size()>0){
				for(CalendarCategory c:list){
					if(!c.getCalendarCatId().equalsIgnoreCase(cat.getCalendarCatId())){
						throw new BaseException(SpringUtils.getMessage("error.duplicate.partner.calendar.category", new Object[]{}));
					}
				}
			}
			dao.clear();
			cat.setModifiedBy(createdUser);
			cat.setModifiedDate(new Date());
			dao.update(cat);
		}catch(BaseException ex){
			ex.printStackTrace();
			throw ex;
		}
		catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.edit.partner.calendar.category", new Object[]{}));
		}
	}
	//delete
	public void deleteCalendarCategory(List<CalendarCategory> deleteList,User createdUser)throws BaseException{
		try{
			for(CalendarCategory c:deleteList){
				if(c.isMarkAsAction()){
					c.setIsDeleted(CommonConstant.IS_DELETED);
					c.setModifiedBy(createdUser);
					c.setModifiedDate(new Date());
					dao.update(c);
				}
			}
		}catch(BaseException ex){
			ex.printStackTrace();
			throw ex;
		}
		catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.delete.partner.calendar.category", new Object[]{}));
		}
	}
	
	
	//partner product calender dtl
	//list
	public List<CalendarDtl> getCalendarDtlList(String catId)throws BaseException{
		try{
			return dao.getCalendarDtlListByCatId(catId);
		}catch(BaseException ex){
			ex.printStackTrace();
			throw ex;
		}
		catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.list.partner.calendar.dtl", new Object[]{}));
		}
	}
	
	//add
	public void createCalendarDtl(CalendarCategory cat,User createdUser,CalendarDtl dtl, Date toDate)throws BaseException{
		try{
			//check duplicate
			if (dtl.getType().equalsIgnoreCase(PartnerConstant.PARTNER_CAL_TYPE_DATE) ){
			 
				Date currentDate =dtl.getCalDate();
				Date startDate =dtl.getCalDate();
				while(DateUtil.isBetweenDate(currentDate, startDate, toDate)){
 
					List<CalendarDtl> list = dao.getCalendarDtlListByName(dtl.getName(), dtl.getDay(), currentDate, cat.getCalendarCatId());
					
					if(list.size()>0){
						throw new BaseException(SpringUtils.getMessage("error.duplicate.partner.calendar.dtl", new Object[]{}));
					} 
					dtl.setCreatedBy(createdUser);
					dtl.setCreatedDate(new Date());
					dtl.setIsDeleted(CommonConstant.NOT_DELETED);
					dtl.setCalCat(cat);
					dtl.setCalDate(currentDate);
					CalendarDtl dtl2 = new CalendarDtl();
					SimpleDateFormat s = new SimpleDateFormat("dd/MM/yyyy");
 
					ReflectionUtil.copyProperties(dtl, dtl2);  
					dtl2.setName(dtl.getName()+"_"+s.format(currentDate));
					dtl2.setCalDate(currentDate);
					dao.save(dtl2);
					currentDate = DateUtil.addDate(currentDate, 1);
				}
			}else{
				List<CalendarDtl> list = dao.getCalendarDtlListByName(dtl.getName(), dtl.getDay(), dtl.getCalDate(), cat.getCalendarCatId());
				if(list.size()>0){
					throw new BaseException(SpringUtils.getMessage("error.duplicate.partner.calendar.dtl", new Object[]{}));
				}
				dtl.setCreatedBy(createdUser);
				dtl.setCreatedDate(new Date());
				dtl.setIsDeleted(CommonConstant.NOT_DELETED);
				dtl.setCalCat(cat);
				dao.save(dtl);
			}
		}catch(BaseException ex){
			ex.printStackTrace();
			throw ex;
		}
		catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.create.partner.calendar.dtl", new Object[]{}));
		}
	}
	
	//edit
	public void updateCalendarDtl(CalendarCategory cat,User createdUser,CalendarDtl dtl)throws BaseException{
		try{
			
			//check duplicate
			List<CalendarDtl> list = dao.getCalendarDtlListByName(dtl.getName(), dtl.getDay(), dtl.getCalDate(), cat.getCalendarCatId());
			if(list.size()>0){
				for(CalendarDtl c:list){
					if(!c.getCalendarDtlId().equalsIgnoreCase(dtl.getCalendarDtlId())){
						throw new BaseException(SpringUtils.getMessage("error.duplicate.partner.calendar.dtl", new Object[]{}));
					}
				}
			}
			dao.clear();
			dtl.setModifiedBy(createdUser);
			dtl.setModifiedDate(new Date());
			dao.update(dtl);
		}catch(BaseException ex){
			ex.printStackTrace();
			throw ex;
		}
		catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.edit.partner.calendar.dtl", new Object[]{}));
		}
	}
	//delete
	public void deleteCalendarDtl(List<CalendarDtl> deleteList,User createdUser)throws BaseException{
		try{
			for(CalendarDtl c:deleteList){
				if(c.isMarkAsAction()){
					c.setIsDeleted(CommonConstant.IS_DELETED);
					c.setModifiedBy(createdUser);
					c.setModifiedDate(new Date());
					dao.update(c);
				}
			}
		}catch(BaseException ex){
			ex.printStackTrace();
			throw ex;
		}
		catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.delete.partner.calendar.dtl", new Object[]{}));
		}
	}
	
	//partner product
	public List<PartnerProduct> getPartnerProductList(Partner partner) throws BaseException{
		try{
			return dao.getPartnerProductList(partner.getPartnerId());
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.delete.partner.calendar.dtl", new Object[]{}));
		}
	}
	
	public void createPartnerProduct(PartnerProduct prod,User user,Partner partner)throws BaseException{
		try{
			
			List<PartnerProduct> list = dao.getPartnerProductListWithName(partner.getPartnerId(), prod.getName());
			if(list.size()>0){
				throw new BaseException("");
			}
			
			prod.setCreatedBy(user);
			prod.setCreatedDate(new Date());
			prod.setPartner(partner);
			prod.setStatus(PartnerConstant.PARTNER_PRODUCT_STATUS_PENDING_APPROVAL);
			prod.setIsDeleted(CommonConstant.NOT_DELETED);
			dao.save(prod);
			
			ActionBulletin ab = new ActionBulletin();
			ab.setActionCode(SpringUtils.getMessage("ab.partner.product.submission", new Object[]{partner.getCode()}));
			ab.setActionNo(SystemUtil.getUniqueCode());
			ab.setActionBean(AdminPartnerProductApprovalBean.BEAN_NAME);
			ab.setActionMethod(AdminPartnerProductApprovalBean.AB_INVOKE_ACTION);
			ab.setActionParam1(prod.getPartnerProductId());
			ab.setIsDeleted(CommonConstant.NOT_DELETED);
			ab.setType(ActionBulletinConstant.AB_ACK_TYPE);
			ab.setCreatedBy(SystemConstant.SYSTEM_USER);
			ab.setCreatedDate(new Date());
			systemService.createActionBulletinByRoles(ab, SystemConstant.ADMIN_ROLE);
			systemService.createActionBulletinByRoles(ab, SystemConstant.MEMBER_SERVICE_ROLE);//Added by Jacky 19-OCT-2008 2133
			
			
		}catch(BaseException ex){
			ex.printStackTrace();
			throw ex;
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.delete.partner.calendar.dtl", new Object[]{}));
		}
	}
	
	public PartnerProduct getPartnerProductById(String id)throws BaseException{
		try{
			return (PartnerProduct)dao.getObject(PartnerProduct.class, id);
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("", new Object[]{}));
		}
	}
	
	public void approvePartnerProduct(Partner partner,PartnerProduct partnerProduct,User user,String code,String reason,String actionNo)throws BaseException{
		try{
			partnerProduct.setCode(code);
			partnerProduct.setReason(reason);
			partnerProduct.setStatus(PartnerConstant.PARTNER_PRODUCT_STATUS_APPROVED);
			partnerProduct.setModifiedBy(user);
			partnerProduct.setModifiedDate(new Date());
			dao.update(partnerProduct);
			
			systemService.deleteActionBulletinByActionNo(actionNo);
			
			ActionBulletin ab = new ActionBulletin();
			ab.setActionCode(SpringUtils.getMessage("ab.partner.product.approved", new Object[]{partnerProduct.getCode()}));
			ab.setActionNo(SystemUtil.getUniqueCode());
			ab.setActionBean(PartnerProductApprovalBean.BEAN_NAME);
			ab.setActionMethod(PartnerProductApprovalBean.AB_APPROVED_INVOKE_METHOD);
			ab.setActionParam1(partnerProduct.getPartnerProductId());
			ab.setIsDeleted(CommonConstant.NOT_DELETED);
			ab.setType(ActionBulletinConstant.AB_ACTION_TYPE);
			ab.setCreatedBy(SystemConstant.SYSTEM_USER);
			ab.setCreatedDate(new Date());
			systemService.createActionBulletinByUser(ab, partner.getRefUser());
			
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("", new Object[]{}));
		}
	}
	
	public void rejectPartnerProduct(Partner partner,PartnerProduct partnerProduct,User user,String reason,String actionNo)throws BaseException{
		try{
			partnerProduct.setReason(reason);
			partnerProduct.setStatus(PartnerConstant.PARTNER_PRODUCT_STATUS_REJECTED);
			partnerProduct.setModifiedBy(user);
			partnerProduct.setModifiedDate(new Date());
			dao.update(partnerProduct);
			
			systemService.deleteActionBulletinByActionNo(actionNo);
			
			ActionBulletin ab = new ActionBulletin();
			ab.setActionCode(SpringUtils.getMessage("ab.partner.product.rejected", new Object[]{}));
			ab.setActionNo(SystemUtil.getUniqueCode());
			ab.setActionBean(PartnerProductApprovalBean.BEAN_NAME);
			ab.setActionMethod(PartnerProductApprovalBean.AB_REJECTED_INVOKE_METHOD);
			ab.setActionParam1(partnerProduct.getPartnerProductId());
			ab.setIsDeleted(CommonConstant.NOT_DELETED);
			ab.setType(ActionBulletinConstant.AB_ACTION_TYPE);
			ab.setCreatedBy(SystemConstant.SYSTEM_USER);
			ab.setCreatedDate(new Date());
			systemService.createActionBulletinByUser(ab, partner.getRefUser());
			
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("", new Object[]{}));
		}
	}
	
	public void ackPartnerProductMessage(String actionNo)throws BaseException{
		try{
			systemService.deleteActionBulletinByActionNo(actionNo);
		}catch(BaseException be){
			throw be;
		}
	}
	
	public Member getMemberByUserId(String userId)throws BaseException{
		try{
			return dao.getMemberByUserId(userId);
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.get.member", new Object[]{}));
		}
	}
	
	public List<PartnerProductBooking> getPartnerProductBookingList(Date from,Date to,String partnerId)throws BaseException{
		try{
			
			if(from!=null&&to!=null){
				Calendar fromCal = GregorianCalendar.getInstance();
				fromCal.setTime(from);
				fromCal.set(Calendar.MINUTE, 0);
				fromCal.set(Calendar.HOUR, 0);
				fromCal.set(Calendar.SECOND, 0);
				from = fromCal.getTime();

				Calendar toCal = GregorianCalendar.getInstance();
				toCal.setTime(to);
				toCal.set(Calendar.MINUTE, 0);
				toCal.set(Calendar.HOUR, 0);
				toCal.set(Calendar.SECOND, 0);
				to = toCal.getTime();
			}
			
			return dao.getPartnerProductBookingList(from, to, partnerId);
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.get.partner.booking.list", new Object[]{}));
		}
	}
	
	public List<PartnerProductExtension> getPartnerProductExtensionList(String productId)throws BaseException{
		try{
			return dao.getPartnerProductExtensionList(productId);
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.get.partner.product.extension.list", new Object[]{}));
		}
	}
	
	public void createPartnerProductExtension(PartnerProductExtension ext,User createdUser)throws BaseException{
		try{
			ext.setCreatedBy(createdUser);
			ext.setIsDeleted(CommonConstant.NOT_DELETED);
			ext.setCreatedDate(new Date());
			dao.save(ext);
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.create.partner.product.extension", new Object[]{}));
		}
	}
	
	public void updatePartnerProfile(Partner partner,User user)throws BaseException{
		try{
			partner.setModifiedBy(user);
			partner.setModifiedDate(new Date());
			dao.update(partner);
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.update.partner.profile", new Object[]{}));
		}
	}
	
	//Added By Jacky 16-03-2008-1100
	public List<PartnerProduct> getPartnerProductApprovedList()throws BaseException{
		List<PartnerProduct> list = new ArrayList<PartnerProduct>();
		try{
			list=dao.getPartnerProductApprovedList();
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.list.object", new Object[]{PartnerProduct.class.getSimpleName()}));
		}
		return list;
	}
	
	public List<PartnerProduct> getPartnerProductApprovedList(String partnerId)throws BaseException{
		List<PartnerProduct> list = new ArrayList<PartnerProduct>();
		try{
			list=dao.getPartnerProductApprovedList(partnerId);
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.list.object", new Object[]{PartnerProduct.class.getSimpleName()}));
		}
		return list;
	}
	
	//Added By Jacky 15-Nov-2008 2218
	public List<XSServiceType> getAllServiceTypeList()throws BaseException{
		List<XSServiceType> list = new ArrayList<XSServiceType>();
		try{
			list = dao.getObjectList(XSServiceType.class);
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.list.object", new Object[]{XSServiceType.class.getSimpleName()}));
		}
		return list;
	}

	public void saveCalendarCategoryDtl(CalendarCategoryDtl calendarCategoryDtl) throws Exception {
		dao.save(calendarCategoryDtl);
	}

	public void deleteCalendarCategoryDtl(List<CalendarCategoryDtl> calCategoryList, User thisUser) throws Exception {
		try{
			for(CalendarCategoryDtl c:calCategoryList){
				if(c.isMarkAsAction()){
					c.setIsDeleted(CommonConstant.IS_DELETED);
					c.setModifiedBy(thisUser);
					c.setModifiedDate(new Date());
					dao.update(c);
				}
			}
		}catch(BaseException ex){
			ex.printStackTrace();
			throw ex;
		}
		catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.delete.partner.calendar.dtl", new Object[]{}));
		}
	}
 
}
